<?php
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require_once './Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Openid')
->setCellValue('B1', '微信昵称')
->setCellValue('C1', '微信头像')
->setCellValue('D1', '奖品类型')
->setCellValue('E1', '多少条鱼')
->setCellValue('F1', '参与时间')
->setCellValue('G1', '券码')
->setCellValue('H1', '领取情况')
;
include '../c/config.php';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$index=2;
$query=$mysqli->query("SELECT * FROM play  ");
$jName=array(
    1=>'一等奖',
    2=>'吃货奖',
    3=>'免单奖',
    4=>'立减2元'
);
while ($list=$query->fetch_assoc()){
	$name=$avatar='';
	$queryU=$mysqli->query("SELECT * FROM users where openid='".$list['openid']."' limit 0,1");
	
	if($queryU->num_rows>0){
	    $getU=$queryU->fetch_array();
	    $name=$getU['nickname'];
	    $avatar=$getU['avatar'];
	    
	}
 
	
	if('yes' == $list['ling1']){
	    $ling= '已领取';
	}else{
	    $ling=  '未领取';
	}
	
	
					$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.$index, $list['openid'])
					->setCellValue('B'.$index, base64_decode($name))
					->setCellValue('C'.$index, $avatar)
					->setCellValue('D'.$index, $jName[$list['jid']])
					->setCellValue('E'.$index, $list['tiao'])
					->setCellValue('F'.$index, $list['createtime'])
					->setCellValue('G'.$index, $ling)
					->setCellValue('H'.$index, $list['thecode'])
					;
					$index++;
					 
					
} 
$mysqli->close();


$objPHPExcel->getActiveSheet()->setTitle('用户信息');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;
?>